﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using OnlineFamilyCalendar.Service;
using OnlineFamilyCalendar.Common.Security;
using OnlineFamilyCalendar.Common.Cryptography;
using OnlineFamilyCalendar.Data;
using OnlineFamilyCalendar.Model;

namespace OnlineFamilyCalendar.Test
{
    public class FakeFamilyMemberService : IFamilyMemberService
    {
        private IOnlineFamilyCalendarEntities _context;
        private IFamilyMemberRepository _repository;
        private const string _encryptionKey = "PCudOGBz8qEf4wUW4L0pZfKJ+wv+cpkod5ZSZRFR/Mk=";

        public FakeFamilyMemberService()
        {
            _context = new FakeOnlineFamilyCalendarEntities();
            _repository = new FamilyMemberRepository(_context);
        }

        public OnlineFamilyCalendarIdentity AuthenticateFamilyMember(string loginName, string password)
        {
            OnlineFamilyCalendarIdentity userIdentity = new OnlineFamilyCalendarIdentity
            {
                Name = loginName,
                IsAuthenticated = false
            };

            FamilyMember familyMember = _repository.GetFamilyMemberByLoginname(loginName);
            if (familyMember == null)
                return userIdentity;

            userIdentity.FamilyMemberId = familyMember.FamilyMemberId;
            userIdentity.Role = familyMember.RoleName;

            familyMember.LoginInformation.Authenticate(password);
            if (familyMember.LoginInformation.IsAuthenticated)
                userIdentity.IsAuthenticated = true;

            return userIdentity;
        }

        public string RegisterNewFamilyMember(string loginName, string password, string emailAddress)
        {
            LoginInformation loginInformation =
                loginInformation = LoginInformation.Create(loginName, password);

            FamilyMember familyMember = FamilyMember.Create(loginInformation);
            familyMember.EmailAddress = emailAddress;

            _repository.AddLoginInformation(loginInformation);
            _repository.AddFamilyMember(familyMember);
            _repository.SaveChanges();

            return GetEncryptedFamilyMemberId(familyMember.FamilyMemberId.ToString());
        }

        public void ActivateFamilyMember(string cypherFamilyMemberIdentifier)
        {
            Guid familyMemberId = new Guid(GetDecryptedFamilyMemberId(cypherFamilyMemberIdentifier));
            FamilyMember familyMember = _repository.GetFamilyMemberByFamilyMemberId(familyMemberId);
            LoginInformation loginInformation = familyMember.LoginInformation;
            loginInformation.Activate();
            _repository.UpdateLoginInformation(loginInformation);
            _repository.SaveChanges();
        }

        public void DeleteFamilyMember(Guid familyMemberId)
        {
            throw new NotImplementedException();
        }

        #region Private Methods
        private string GetEncryptedFamilyMemberId(string familyMemberId)
        {
            EncryptionHelper encryptionHelper = new EncryptionHelper(_encryptionKey);
            return encryptionHelper.EncryptData(familyMemberId);
        }

        private string GetDecryptedFamilyMemberId(string encryptedFamilyMemberId)
        {
            EncryptionHelper encryptionHelper = new EncryptionHelper(_encryptionKey);
            return encryptionHelper.DecryptData(encryptedFamilyMemberId);
        }
        #endregion
    }
}
